package com.bac.bacplatform;

import java.util.List;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.bac.utils.LockPatternUtils;
import com.bac.utils.LockPatternView;
import com.bac.utils.Util;
import com.bac.utils.LockPatternView.Cell;
import com.bac.utils.LockPatternView.DisplayMode;
import com.bac.utils.LockPatternView.OnPatternListener;

public class LockActivity extends BaseActivity {
	private LockPatternView lockPatternView;
	private LockPatternUtils lockPatternUtils;
	private String patternPwd;
	private int firstSet = 0;

	private String mAction;
	private int mWrongCount = 5;
	
	private boolean modify_second = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lock);
		findViewById(R.id.title_left).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finishActivityOut();
			}
		});
		mAction = getIntent().getStringExtra("action");
		Log.i(getClass().getName()+"----action", mAction);
		if("modify_gesture".equals(mAction)){
			((TextView)findViewById(R.id.lock_title)).setText("修改手势密码");
			findViewById(R.id.lock_tip).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.lock_tip)).setText("请输入原手势");
		}else if("close_gesture".equals(mAction)){
			((TextView)findViewById(R.id.lock_title)).setText("验证手势密码");
			findViewById(R.id.lock_tip).setVisibility(View.VISIBLE);
			((TextView)findViewById(R.id.lock_tip)).setText("请输入原手势");
		}
	}

	@Override
	public void init() {
		lockPatternView = (LockPatternView) findViewById(R.id.lpv_lock);
		lockPatternUtils = new LockPatternUtils(this);
		lockPatternView.setOnPatternListener(new OnPatternListener() {

			public void onPatternStart() {

			}

			public void onPatternDetected(final List<Cell> pattern) {
				if ("set_gesture".equals(mAction)) {
					if (firstSet == 0) {
						showToast("请再次滑动手势");
						patternPwd = LockPatternUtils.patternToString(pattern);
						lockPatternView.clearPattern();
					} else {
						if (patternPwd.equals(LockPatternUtils
								.patternToString(pattern))) {
							showToast("手势密码设置完成");
							lockPatternView.setEnabled(false);
							lockPatternUtils.saveLockPattern(pattern);
							new Handler().postDelayed(new Runnable() {
								@Override
								public void run() {
									finish();
								}
							}, 1000);
						} else {
							showToast("手势不一致，请重新滑动手势");
							lockPatternView.setDisplayMode(DisplayMode.Wrong);
							new Handler().postDelayed(new Runnable() {
								@Override
								public void run() {
									lockPatternView.clearPattern();
								}
							}, 500);
						}
					}
					firstSet++;
				}else if("modify_gesture".equals(mAction)){
					if (!modify_second) {
						int result = lockPatternUtils.checkPattern(pattern);
						if (result != 1) {
							lockPatternView.setDisplayMode(DisplayMode.Wrong);
							if (mWrongCount > 1) {
								mWrongCount--;
								((TextView)findViewById(R.id.lock_tip)).setTextColor(Color.RED);
								((TextView)findViewById(R.id.lock_tip)).setText("还可以输入"+mWrongCount+"次");
								new Handler().postDelayed(new Runnable() {
									@Override
									public void run() {
										lockPatternView.clearPattern();
									}
								}, 500);
							}else{
//								showToast("哈哈，没机会了");
								((TextView)findViewById(R.id.lock_tip)).setText("还可以输入0次");
								lockPatternUtils.clearLock();
								GogoLoginDialog("提示", "请用口令重新登录，在个人中心重新设置手势密码");
							}
						}else{
							((TextView)findViewById(R.id.lock_tip)).setTextColor(Color.WHITE);
							((TextView)findViewById(R.id.lock_tip)).setText("输入新的手势");
							lockPatternView.clearPattern();
							modify_second = true;
						}
					}else{
						showToast("手势密码修改完成");
						lockPatternUtils.saveLockPattern(pattern);
						lockPatternView.setEnabled(false);
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								finish();
							}
						}, 1000);
					}
				}else if("close_gesture".equals(mAction)){
					int result = lockPatternUtils.checkPattern(pattern);
					if (result != 1) {
						lockPatternView.setDisplayMode(DisplayMode.Wrong);
						if (mWrongCount > 1) {
							mWrongCount--;
							((TextView)findViewById(R.id.lock_tip)).setTextColor(Color.RED);
							((TextView)findViewById(R.id.lock_tip)).setText("还可以输入"+mWrongCount+"次");
							new Handler().postDelayed(new Runnable() {
								@Override
								public void run() {
									lockPatternView.clearPattern();
								}
							}, 500);
						}else{
//							showToast("哈哈，没机会了");
							((TextView)findViewById(R.id.lock_tip)).setText("还可以输入0次");
							lockPatternUtils.clearLock();
							GogoLoginDialog("提示", "请用口令重新登录，在个人中心重新设置手势密码");
						}
					}else{
						lockPatternUtils.clearLock();
						showToast("手势密码已关闭");
						new Handler().postDelayed(new Runnable() {
							@Override
							public void run() {
								finish();
							}
						}, 1000);
					}
				}
				

			}

			public void onPatternCleared() {
				Log.i("lock cleared", "cleared locked!");
			}

			public void onPatternCellAdded(List<Cell> pattern) {

			}
		});
	}
	
	public void GogoLoginDialog(String title, String message){
		AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Custom_dialog);
		final AlertDialog alertDialog = builder.create();
//		alertDialog.setCanceledOnTouchOutside(false);
		alertDialog.show();
		Window window = alertDialog.getWindow();
		View view = LayoutInflater.from(this).inflate(R.layout.dialog_alert, null);
		((TextView)(view.findViewById(R.id.tv_title))).setText(title);
		((TextView)(view.findViewById(R.id.tv_message))).setText(message);
		view.findViewById(R.id.ly_ok).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.i(getClass().getName(), "重新登录重置手势密码");
				Intent intent = new Intent(LockActivity.this, LoginActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				alertDialog.dismiss();
				BacApplication.getInstance().exit();
				finish();
			}
		});
		window.setContentView(view);
		window.setLayout((int)(Util.getWidth(getApplicationContext()) * 0.8), WindowManager.LayoutParams.WRAP_CONTENT);
	}

}
